package top.psjj.news.web;

import top.psjj.news.po.TComment;
import top.psjj.news.service.CommentService;
import top.psjj.news.service.impl.CommentServiceImpl;
import top.psjj.news.utils.ServiceProxyFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Auther:小陈的Java
 */
@WebServlet("/comment")
public class CommentServlet extends BasicServlet{
    private CommentService commentService = (CommentService) ServiceProxyFactory.getService(CommentServiceImpl.class);
    private String mainUrl = "/WEB-INF/jsp/main/mainTemp.jsp";
    private String listUrl = "./comment/commentList.jsp";
    public void list(HttpServletRequest req, HttpServletResponse resp) throws Exception{

        //获得查询条件
        String s_bCommentDate = req.getParameter("s_bCommentDate");
        String s_aCommentDate = req.getParameter("s_aCommentDate");
        //获得当前页
        int currentPage = req.getParameter("currentPage")==null?1:
                Integer.parseInt(req.getParameter("currentPage"));
        //指定每页显示
        int pageSize = 10;
        //获得查询起点，
        /**
         * 1   0-9
         * 2   10-19
         * 3   20-29
         */
        //查询总记录数
        int count = commentService.count(s_bCommentDate,s_aCommentDate);
        //总页数
        int pageNum = (int)Math.ceil(count/(double)pageSize);
        if(pageNum<1){
            pageNum=1;
        }
        if(currentPage<1){
            currentPage=1;
        }
        if(currentPage>pageNum){
            currentPage=pageNum;
        }
        int begin = (currentPage-1)*pageSize;



        //查询数据
        //List<TComment> list =  commentService.list(s_bCommentDate,s_aCommentDate);
        List<TComment> list = commentService.page(s_bCommentDate,s_aCommentDate,begin,pageSize);

        //存储数据
        req.setAttribute("list",list);
        req.setAttribute("currentPage",currentPage);
        req.setAttribute("pageSize",pageSize);
        req.setAttribute("count",count);
        req.setAttribute("pageNum",pageNum);
        req.setAttribute("mainPage",listUrl);
        //转发页面
        req.getRequestDispatcher(mainUrl).forward(req,resp);
    }
}
